package me.luger.project.fof.service;

import me.luger.core.dao.nutz.service.BaseService;
import me.luger.project.fof.entity.FofFundIndicator;
import org.apache.commons.collections.ArrayStack;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class FofFundIndicatorService extends BaseService<FofFundIndicator> {
    public FofFundIndicatorService(Dao dao) {
        super(dao);
    }

    public List<List<Object>> getRangeProfit(Date startDate,Date endDate,Integer fid){
        Sql sql = Sqls.create("select navDate,unitNav from fof_fund_indicator where fId = @fId and navDate >= @startDate and navDate <= @endDate");
        sql.setParam("fId",fid);
        sql.setParam("startDate",startDate);
        sql.setParam("endDate",endDate);
        List<FofFundIndicator> fofFundIndicatorList = this.list(sql);

        List<List<Object>> rangeProfitList = new ArrayList<List<Object>>();
        List<Object> list = null;
        for(FofFundIndicator fofFundIndicator : fofFundIndicatorList){
            list = new ArrayList();
            list.add(fofFundIndicator.getNavDate().getTime());
            list.add(fofFundIndicator.getUnitNav());
            rangeProfitList.add(list);
        }
        return rangeProfitList;
    }

    /**
     * 获取一个FOF的最大回撤
     * @param fofId
     * @return
     */
    public List<FofFundIndicator> getDrawDown(Integer fofId, Integer extSource){
        if(fofId == null) {
            return null;
        }
        String fofStr = "00000" + fofId.toString() + ".FOF";
        //FOF在fund_nav中以0000XX.FOF格式存储
        Integer fofStrLen = 10;
        if(fofStr.length() > fofStrLen) {
            fofStr = fofStr.substring(fofStr.length() - fofStrLen);
        }
        Sql sql = Sqls.create("select drawdown, navDate from fof_fund_indicator where ext_code = @extCode and ext_source = @extSource ");
        sql.setParam("extCode",fofStr);
        sql.setParam("extSource",extSource);
        List<FofFundIndicator> fofFundIndicatorList = this.list(sql);
        if(fofFundIndicatorList == null || fofFundIndicatorList.size() < 1) return null;
        return  fofFundIndicatorList;
    }
}
